﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.ServiceModel.DomainServices.Server;
using System.Text;

namespace Arch.DomainServices.Metadata
{
    public class DomainMetadataProvider : DomainServiceDescriptionProvider
    {
        public DomainMetadataProvider(Type domainServiceType, DomainServiceDescriptionProvider parent)
            : base(domainServiceType, parent)
        {
            EntityBuilders = new Dictionary<Type, EntityBuilder>();
        }

        public bool IsConfigured { get; set; }

        internal void Register(EntityBuilder entityBuilder)
        {
            EntityBuilders.Add(entityBuilder.Type, entityBuilder);
        }

        public IDictionary<Type, EntityBuilder> EntityBuilders { get; protected set; }

        public override ICustomTypeDescriptor GetTypeDescriptor(Type type, ICustomTypeDescriptor parent)
        {
            if (EntityBuilders.ContainsKey(type) == false)
                return parent;

            return new MetadataTypeDescriptor(EntityBuilders[type].Attributes, type, parent);
        }
    }
}
